package cn.tedu.bonfire.upload.qiniuyunutil;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.stereotype.Component;


@Component
public class FileUploadUtil {

    public static String uploadVideo(byte[] bytes,String fileName){
        String bucket = "bonfire11";
        final String qiNiuPost = uploadByBytes(bytes, fileName,bucket);
        if (qiNiuPost == null) {
            return null;


        }
        return "http://s6q7t61j8.hd-bkt.clouddn.com/" + qiNiuPost;
    }


    public static String uploadImage(byte[] bytes,String fileName){
        String bucket = "bonfire-img";
        final String qiNiuPost = uploadByBytes(bytes, fileName,bucket);
        if (qiNiuPost == null) {
            return null;
        }
        return "http://s6q7t61j8.hd-bkt.clouddn.com/" + qiNiuPost;
    }

    private static String uploadByBytes(byte[] bytes,String fileName,String bucket){

        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.huadong());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
//...其他参数参考类注释

        String accessKey = "OikwxDQY4vAP82O9PAIfyRsppjZRIOe0Y_5QMdQg";
        String secretKey = "bBGy5yUdSwH-ciA1UTzfFWGpDPnDRVYIVQS2CmX8";

        UploadManager uploadManager = new UploadManager(cfg);

//默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = fileName;

        try {
            //byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);

            try {
                Response response = uploadManager.put(bytes, key, upToken);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//                System.out.println(putRet.key);
//                System.out.println(putRet.hash);
                return putRet.key;
            } catch (QiniuException ex) {
                ex.printStackTrace();
                if (ex.response != null) {
                    System.err.println(ex.response);

                    try {
                        String body = ex.response.toString();
                        System.err.println(body);
                    } catch (Exception ignored) {
                    }
                }
            }
        } catch (Exception ex) {
            //ignore
        }

        return null;

    }

    public static String deleteImage(String fileName){
        String bucket = "bonfire-img";
        return deleteFile(bucket, fileName);
    }

    public static String deleteVideo(String fileName){
        String bucket = "bonfire-img";
        return deleteFile(bucket, fileName);
    }
    public static String deleteFile(String bucket,String fileName){
        Configuration cfg = new Configuration(Region.region0());
//...其他参数参考类注释
        String accessKey = "OikwxDQY4vAP82O9PAIfyRsppjZRIOe0Y_5QMdQg";
        String secretKey = "bBGy5yUdSwH-ciA1UTzfFWGpDPnDRVYIVQS2CmX8";
//        String bucket = "your bucket name";
//        String key = "your file key";
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, fileName);
            return "刪除成功";
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
        return "刪除失敗";
    }
}
